package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import Shared.AmbState;
import Shared.MapPosition;
import Simulator.AmbulanceSimulator;
import Simulator.RealAmbulance;
import Simulator.ER;
import Simulator.EnvironmentDatabase;

public class MapGUI extends JPanel implements KeyListener {

	private String londra = "londra.jpg";
	private Image mybackground;
	private Image highlightpin;
	private Set<MapPosition> highlightedPositions;
	private Image victimGraphic, victimGraphicUrgent;
	private Image hospitalGraphic;

	private EnvironmentDatabase db;

	private Image ambGraphic, ambGraphicBusy, ambGraphicError, ambGraphicLogout, ambGraphicOntheway,
			ambGraphicUnregistered, ambGraphicWaiting;
	private Image ambGraphicUrgent, ambGraphicUrgentBusy, ambGraphicUrgentError, ambGraphicUrgentLogout,
			ambGraphicUrgentOntheway;
	private Image ambGraphicUrgentUnregistered, ambGraphicUrgentWaiting;

	public MapGUI() {
		this.db = EnvironmentDatabase.getInstance();

		ambGraphic = (new ImageIcon(this.getClass().getResource("amb2.png"))).getImage();
		ambGraphicBusy = (new ImageIcon(this.getClass().getResource("amb2_busy.png"))).getImage();
		ambGraphicError = (new ImageIcon(this.getClass().getResource("amb2_error.png"))).getImage();
		ambGraphicLogout = (new ImageIcon(this.getClass().getResource("amb2_logout.png"))).getImage();
		ambGraphicOntheway = (new ImageIcon(this.getClass().getResource("amb2_ontheway.png"))).getImage();
		ambGraphicUnregistered = (new ImageIcon(this.getClass().getResource("amb2_unregistered.png"))).getImage();
		ambGraphicWaiting = (new ImageIcon(this.getClass().getResource("amb2_waiting.png"))).getImage();

		ambGraphicUrgent = (new ImageIcon(this.getClass().getResource("AmbulanceEquip-basic.png"))).getImage();
		ambGraphicUrgentBusy = (new ImageIcon(this.getClass().getResource("AmbulanceEquip_busy.png"))).getImage();
		ambGraphicUrgentError = (new ImageIcon(this.getClass().getResource("AmbulanceEquip_error.png"))).getImage();
		ambGraphicUrgentLogout = (new ImageIcon(this.getClass().getResource("AmbulanceEquip_logout.png"))).getImage();
		ambGraphicUrgentOntheway = (new ImageIcon(this.getClass().getResource("AmbulanceEquip_ontheway.png")))
				.getImage();
		ambGraphicUrgentUnregistered = (new ImageIcon(this.getClass().getResource("AmbulanceEquip_unregistered.png")))
				.getImage();
		ambGraphicUrgentWaiting = (new ImageIcon(this.getClass().getResource("AmbulanceEquip_waiting.png"))).getImage();

		victimGraphic = (new ImageIcon(this.getClass().getResource("victim.png"))).getImage();
		victimGraphicUrgent = (new ImageIcon(this.getClass().getResource("victimCircled.png"))).getImage();
		hospitalGraphic = (new ImageIcon(this.getClass().getResource("hosp.png"))).getImage();

		highlightedPositions = new HashSet<MapPosition>();

		mybackground = (new ImageIcon(this.getClass().getResource(londra))).getImage();

		setFocusable(true);
		setBackground(Color.BLACK);
		setDoubleBuffered(true);

		addMouseListener(new MapGUIListener(this));
		addKeyListener(this);

	}

	public void actionPerformed(ActionEvent e) {

	}

	public void paint(Graphics g) {
		super.paint(g);

		Graphics2D g2d = (Graphics2D) g;

		g2d.drawImage(mybackground, 0, 0, null);

		for (MapPosition hospital : db.getHospitals())
			g2d.drawImage(this.hospitalGraphic, (int) (hospital.getPoint().getX()) - 24,
					(int) (hospital.getPoint().getY()) - 24, this);

		/* repaint ambulance */
		for (RealAmbulance realAmb : db.getAllAmbulances()) {

			int x = ((int) realAmb.getPosition().getPoint().getX() - 48 / 2);
			int y = ((int) realAmb.getPosition().getPoint().getY() - 48 / 2);
			AmbState tempState = realAmb.getStatus();
			if (realAmb.isEquiped()) {
				if (tempState == AmbState.UNREGISTERED)
					g.drawImage(ambGraphicUrgentUnregistered, x, y, this);
				else if (tempState == AmbState.INACTIVE)
					g.drawImage(ambGraphicUrgentLogout, x, y, this);
				else if (tempState == AmbState.INACTIVE)
					g.drawImage(ambGraphicUrgentLogout, x, y, this);
				else if (tempState == AmbState.WAIT_FOR_MISSION)
					g.drawImage(ambGraphicUrgentWaiting, x, y, this);
				else if (tempState == AmbState.BUSY)
					g.drawImage(ambGraphicUrgentBusy, x, y, this);
				else if (tempState == AmbState.ERROR)
					g.drawImage(ambGraphicUrgentError, x, y, this);
				else if (tempState == AmbState.ON_ITS_WAY)
					g.drawImage(ambGraphicUrgentOntheway, x, y, this);
				else
					g.drawImage(ambGraphicUrgent, x, y, this);
			} else {
				if (tempState == AmbState.UNREGISTERED)
					g.drawImage(ambGraphicUnregistered, x, y, this);
				else if (tempState == AmbState.INACTIVE)
					g.drawImage(ambGraphicLogout, x, y, this);
				else if (tempState == AmbState.INACTIVE)
					g.drawImage(ambGraphicLogout, x, y, this);
				else if (tempState == AmbState.WAIT_FOR_MISSION)
					g.drawImage(ambGraphicWaiting, x, y, this);
				else if (tempState == AmbState.BUSY)
					g.drawImage(ambGraphicBusy, x, y, this);
				else if (tempState == AmbState.ERROR)
					g.drawImage(ambGraphicError, x, y, this);
				else if (tempState == AmbState.ON_ITS_WAY)
					g.drawImage(ambGraphicOntheway, x, y, this);
				else
					g.drawImage(ambGraphic, x, y, this);
			}
		}

		for (ER e : db.getAllER()) {
			int x = ((int) e.getLocation().getPoint().getX() - 48 / 2);
			int y = ((int) e.getLocation().getPoint().getY() - 48 / 2);
			g.drawImage(victimGraphic, x, y, this);
			if (e.isUrgent())
				g.drawImage(victimGraphicUrgent, x, y, this);
			else
				g.drawImage(victimGraphic, x, y, this);
		}

		// On affiche les points Highlighted
		Iterator<MapPosition> iter = this.highlightedPositions.iterator();
		while (iter.hasNext()) {
			MapPosition temp = (MapPosition) iter.next();
			int[] c = temp.getCoordinates();
			g2d.drawImage(highlightpin, ((int) c[0] - 2), ((int) c[1] - 2), this);
		}

		/* other stuff */
		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		AmbulanceSimulator temp = AmbulanceSimulator.getInstance();

		if (arg0.getKeyCode() == KeyEvent.VK_SPACE) {
			synchronized (temp) {
				temp.setHalted(!temp.isHalted());
			}

		}

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
	}

}